package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @Description: 成本中心（部门） 控制层
 * @Author: China.wsg
 * @Date: 2020/05/11 10:32
 */
@Controller
@RequestMapping("/basic/dept")
public class TDeptController extends BaseController {

    @Autowired
    private TDeptService deptService;
    @Autowired
    private LogService logService;


    /**
     * 成本中心 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:dept:view"})
    public String page() {
        return "basic/dept/page";
    }

    /**
     * 成本中心列表
     *
     * @return 成本中心 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:dept:view"})
    public List<TDept> list(TDept dept) {
        return deptService.findDeptInfo(dept);
    }

    /**
     * 创建部门
     *
     * @param dept 部门信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:dept:edit"})
    public JsonResult save(TDept dept) {
        logService.saveLog("新建部门", dept.getDepName());
        return jsonResult(deptService.saveDept(dept));
    }

    /**
     * 查询部门
     *
     * @param id 部门id
     * @return 部门
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"basic:dept:edit"})
    public TDept id(Integer id) {
        return deptService.findDeptById(id);
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:dept:edit"})
    public JsonResult update(TDept dept) {
        TDept r = deptService.findDeptById(dept.getDepId());
        logService.saveLog("修改部门", r, dept);
        return jsonResult(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     *
     * @param dept 部门信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:dept:edit"})
    public JsonResult del(TDept dept) {
        TDept d = deptService.findDeptById(dept.getDepId());
        logService.saveLog("删除部门", d.getDepName());
        return jsonResult(deptService.delDept(dept));
    }

    /**
     * 名称唯一验证
     *
     * @param r 角色
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    public ValidResult checkUnique(TDept r) {
        TDept dept = deptService.findDeptByName(r.getDepName());
        if (r.getDepId() == null) {
            return ValidResult.result(dept);
        }
        if (dept == null || dept.getDepId().equals(r.getDepId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }


}
